<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .content .content_nav {
            width: 90px;
            height: 100%;
            background-color: #f8f8f8;
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: scroll;
            -webkit-overflow-scrolling: touch;
        }
        .content .content_nav::-webkit-scrollbar{
          display: none;
        }
        .content .content_nav ul li {
            /*height: 45px;*/
            /*line-height: 45px;*/
            border-bottom: 1px solid #fff;
            font-size: 14px;
            padding: 12px 12px;
            color: #333;
            background-color: #f8f8f8;
            overflow: hidden;
            /*text-overflow: ellipsis;*/
            /*white-space: nowrap;*/
            text-align: center;
        }
        .content .content_nav ul li.active {
            background-color: #fff;
        }
        .content .content_main {
            height: 100%;
            background-color: #fff;
            padding-left: 10px;
            position: fixed;
            left: 90px;
            right: 0;
            top: 0;
            bottom: 0;
            overflow-y: scroll;
            -webkit-overflow-scrolling: touch;
        }
        .content .content_main::-webkit-scrollbar{
          display: none;
        }
        .content .content_main ul li .li_box {
            height: 75px;
            border-bottom: 1px solid #e8e8e8;
            overflow: hidden;
            display: -webkit-box;
            display: -webkit-flex;
        }
        .content .content_main ul li .li_box .li_boxl {
            width: 0;
            height: 75px;
            padding-top: 10px;
            padding-bottom: 10px;
            box-sizing: border-box;
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
            display: -webkit-box;
            display: -webkit-flex;
            align-items: center;
        }
        .content .content_main ul li .li_box .li_boxl .li_img .img_box {
            width: 55px;
            height: 55px;
            border-radius: 5px;
            overflow: hidden;
        }
        .content .content_main ul li .li_box .li_boxl .li_img .img_box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .content .content_main ul li .li_box .li_boxl .li_info {
            width: 0;
            padding-left: 10px;
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
            padding-right: 5px;
        }
        .content .content_main ul li .li_box .li_boxl .li_info .title {
            font-size: 15px;
            color: #333;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .content .content_main ul li .li_box .li_boxl .li_info .desc {
            font-size: 12px;
            color: #999;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .content .content_main ul li .li_box .li_boxl .li_info .price {
            color: #e62442;
            font-size: 16px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .content .content_main ul li .li_box .li_boxr {
            height: 75px;
            padding-right: 15px;
            padding-top: 12px;
            font-size: 12px;
            color: #999;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="content_nav">
            <ul id="menu_ul">
                <li class="active" tapmode onclick="fnClick(0);"></li>

            </ul>
        </div>
        <div class="content_main" id="goods_box">
            <ul id="goods_ul">
           <!--      <li>
                    <div class="li_box">
                        <div class="li_boxl">
                            <div class="li_img">
                                <div class="img_box">
                                    <img src="../image/temp/1.png">
                                </div>
                            </div>
                            <div class="li_info">
                                <div class="title">菜品名称</div>
                                <div class="desc">菜品介绍</div>
                                <div class="price" data="20">RM20</div>
                            </div>
                        </div>
                        <div class="li_boxr">库存681</div>
                    </div>
                </li> -->

            </ul>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="catsTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li id="cat_{{=it[i]['id']}}" cat_id="{{=it[i]['id']}}" {{if(i==0){ }} class="active"{{ }; }} tapmode onclick="fnClick({{=i}},{{=it[i]['id']}});">{{=it[i]['category_name']}}</li>

    {{ }; }}
</script>
<script id="goodsTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <ul id="goods_ul_{{=it[i]['cat_id']}}" style="display: {{if(i==0){ }}block; {{ }else{  }} none; {{ };  }}">
            {{for(var j=0;j<it[i]['goods'].length;j++){ }}
                <li onclick="fnEditGoods({{=it[i]['goods'][j]['id']}});" tapmode>
                    <div class="li_box" >
                        <div class="li_boxl">
                            <div class="li_img">
                                <div class="img_box">
                                    <img src="../image/default.png">
                                    <img onload="setImg(this);" class="hidden_img" src="{{=formatVideoImg(it[i]['goods'][j]['goods_img'])}}">
                                </div>
                            </div>
                            <div class="li_info">
                                <div class="title">{{=it[i]['goods'][j]['goods_name']}}</div>
                                <div class="desc">{{=it[i]['goods'][j]['desc']}}</div>
                                <div class="price" data="20">RM{{=it[i]['goods'][j]['price']}}</div>
                            </div>
                        </div>
                        <div class="li_boxr">库存{{=it[i]['goods'][j]['stock']}}</div>
                    </div>
                </li>
            {{ }; }}
        </ul>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        shop = $api.getStorage('shop_data');
        now_cat = '';
        cat_index = 0;
        openLoading();
        fnGetshopGoods();

        // 下拉刷新
        api.setCustomRefreshHeaderInfo({
            bgColor: '#f8f8f8',
            loadAnimInterval:60,
            image: {
               pull: pullArr,
               load: loadArr
            }
        }, function(ret, err) {
            fnGetshopGoods(1);
            api.refreshHeaderLoadDone();
        });

        api.addEventListener({
            name: 'goodsChangeEvent'
        }, function(ret, err) {
            if (ret) {
                fnGetshopGoods(1);
            }
        });

        api.addEventListener({
            name: 'loginEvent'
        }, function(ret, err) {
            if (ret) {
                fnGetshopGoods(1);
            }
        });
    };

        // 菜品详情
    function fnEditGoods(id) {
        api.openWin({
            name: 'add_goods',
            url: './add_goods.html',
            pageParam : {
                goods_id:id
            }
        });
    }

    function fnClick(_index,cat_id) {
        fnChange(_index);
        cat_index = _index;
        if(now_cat != cat_id) {
            $('#goods_box ul').hide();
            $('#goods_ul_'+cat_id).show();
            now_cat = cat_id;
        }
    }

    function fnChange(_index) {
        if (!isNaN(_index)) {
            this_ = $('.content_nav ul li').eq(_index);
        }
        $('.content_nav ul li').removeClass('active');
        $(this_).addClass('active');
        var scroll = $(this_).offset().top + $('.content_nav').scrollTop() - $('.content_nav').height() / 2 + $(this_).height() / 2;
        $('.content_nav').scrollTop(scroll);
    }

    function fnGetshopGoods(type) {
        shop = $api.getStorage('shop_data');
        api.ajax({
            url: rootPath+'/api/shop/get_shop_goods',
            method: 'get',
            data: {
                values: { 
                    shop_id: shop.id
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(type == 1) {
                    // now_cat = '';
                    $('#menu_ul').empty();
                    $('#goods_box').empty();
                }
                if(ret.cats) {
                    if(ret['cats'].length > 0) {
                        now_cat = ret['cats'][0]['id'];
                    }
                    if(ret['cats']) {
                        setDotData(ret.cats,'catsTpl','menu_ul',true);
                    }
                    if(ret['goods']) {
                        setDotData(ret.goods,'goodsTpl','goods_box',true);
                        fnChange(0);
                    }
                }
            } else {
                showError(err.code,err.msg);
            }
            //刷新时候还原原来的状态
            if(type == 1) {
                var refresh_cat_id = $('#menu_ul li').eq(cat_index).attr('cat_id');
                fnClick(cat_index,refresh_cat_id);
            }
            closeLoading();
        });
    }
</script>
</html>